package just.stuff.morsels.ui.user;

import javax.servlet.http.Cookie;

import just.stuff.morsels.MorselSession;

import org.apache.wicket.RequestCycle;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.protocol.http.WebRequest;
import org.apache.wicket.protocol.http.WebResponse;

public final class LogoutLink extends Link<Void> {
	public LogoutLink(String id) {
		super(id);
	}

	@Override
	public void onClick() {
		MorselSession.get().setUser( null );
		Cookie cookie = new Cookie( "user", null );
		cookie.setMaxAge( 0 );
		String context = ((WebRequest)RequestCycle.get().getRequest()).getHttpServletRequest().getContextPath();
		if( context == null || context.length() == 0 )
			context = "/";
		cookie.setPath( context );
		((WebResponse)RequestCycle.get().getResponse()).addCookie( cookie );
		//TODO: redirect to current page
	}

	@Override
	public boolean isVisible() {
		return MorselSession.isSignedIn();
	}
}